package ltd.hxya.novel.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.Channel;
import com.xxl.job.core.handler.annotation.XxlJob;
import ltd.hxya.novel.admin.entity.OperLog;
import ltd.hxya.novel.admin.mapper.OperLogMapper;
import ltd.hxya.novel.admin.service.IOperLogService;
import ltd.hxya.novel.common.constant.rabbit.RabbitConstant;
import ltd.hxya.novel.common.utils.BaseUtils;
import ltd.hxya.novel.common.utils.PageUtils;
import ltd.hxya.novel.common.vo.PageParam;
import ltd.hxya.novel.common.anno.RabbitAck;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Service
public class OperLogServiceImpl extends ServiceImpl<OperLogMapper, OperLog> implements IOperLogService {

    @Autowired
    private OperLogMapper operLogMapper;

    @RabbitListener(bindings =@QueueBinding(
            value = @Queue(RabbitConstant.OPER_LOG_ADD_QUEUE),
            exchange = @Exchange(RabbitConstant.OPER_LOG_ADD_EXCHANGE),
            key = RabbitConstant.OPER_LOG_ADD_ROUTING_KEY
    ))
    @RabbitHandler
    @RabbitAck
    public void addOperLog(OperLog operLog, Message message, Channel channel) throws IOException {
        operLog.setOperId(IdWorker.getIdStr());
        this.baseMapper.insert(operLog);
        //channel.basicAck(deliveryTag,true);
    }

    @Override
    public Page<OperLog> operlogPageList(OperLog operLog) {
        QueryWrapper<OperLog> operLogQueryWrapper = new QueryWrapper<>();
        BaseUtils.compareTime(operLogQueryWrapper,operLog,"oper_time");
        PageParam pageParams = PageUtils.getPageParams();
        if (!StringUtils.isEmpty(operLog.getTitle())){
            operLogQueryWrapper.like("title",operLog.getTitle());
        }
        if (!StringUtils.isEmpty(operLog.getMethod())){
            operLogQueryWrapper.eq("method",operLog.getMethod());
        }
        if (operLog.getStatus()!=null){
            operLogQueryWrapper.eq("status",operLog.getStatus());
        }
        if (operLog.getOperatorType()!=null){
            operLogQueryWrapper.eq("",operLog.getOperatorType());
        }

        operLogQueryWrapper.orderByDesc("oper_time");
        Page<OperLog> page = this.page(new Page<OperLog>(pageParams.getCurrent(), pageParams.getSize()),operLogQueryWrapper);
        return page;
    }

    @Override
    public void deleteLogs(String[] ids) {
        if (ids!=null&&ids.length>0){
            List<String> idList = Arrays.asList(ids);
            operLogMapper.deleteBatchIds(idList);
        }
    }

    /**
     * 定期清理正常执行的日志信息
     */
    @XxlJob("clearLog")
    public void clearLog(){
        QueryWrapper<OperLog> operLogQueryWrapper = new QueryWrapper<>();
        operLogQueryWrapper.eq("status",1);
        this.baseMapper.delete(operLogQueryWrapper);
    }

    //定期清理所有的日志信息
    @XxlJob("clearAllLog")
    public void deleteAllLog(){
        this.baseMapper.delete(null);
    }
}
